<?php

/**
 * 首页
 * User: tsang
 * Date: 2017/9/14
 * Time: 20:11
 */

namespace app\admin\Controller;
use think\Db;
use think\Request;

class Voucher extends Common
{
	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();
	}

	/*
	*	优惠卷列表
	*/
    public function lists()
    {	
    	$where=array();
    	$where['delete'] = 0;
    	$keyword = input('keyword');

		if($keyword){
			$where['name']=array("like",'%'.$keyword.'%');
		}

		$status = input('status');

		if(!empty($status)){
			if($status == '-1'){
				$status = 0;
			}
			$where['status']=$status;
		}

		$list = db("voucher")->where($where)->order("id desc")->field("id, image, fullmoney, name, deduction, count, status")->paginate(10);

    	$this->assign('keyword',$keyword);
    	$this->assign('status',$status);
    	$this->assign('list',$list);
        return $this->fetch();
    }

	
	/*
	*	添加优惠卷
	 */
	public function add_voucher()
    {	
    	$id = input('id/d');
		$data = array();

		if(request()->isPost()){
			$data['name']=input("name");
			$data['fullmoney']=input("fullmoney");
			$data['count']=input("count");
			$data['deduction']=input("deduction");
			$data['useful_day']=input("useful_day");
			$data['create_time']=time();

			if(!empty($id)){
				$data['status'] = input("status");
				$res=db("voucher")->where("id='$id'")->update($data);
			}else{
				$data['status'] = 1;
				$res=db("voucher")->insert($data);
			}
			if(!$res) $this->error("操作失败！");
			$this->redirect(url('voucher/lists'));
		}

		$voucher_result = db("voucher")->where("id='$id'")->find();

    	$this->assign('voucher_result',$voucher_result);
    	return $this->fetch();
    }

    //删除优惠卷
    public function delete_voucher()
    {
    	$id = input('id/d');

    	$data['delete'] = 1;
		$res=db("voucher")->where("id='$id'")->update($data);
    	
    	$this->redirect(url('Voucher/lists'));
    }
}